/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.paris5.master2.guitarmaster.bean.EcomComment;
import com.paris5.master2.guitarmaster.bean.EcomProductType;
import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomQueryException;
import com.paris5.master2.guitarmaster.service.ServiceConvertorRemote;
import com.paris5.master2.guitarmaster.service.ServiceProductRemote;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Alice
 */
public class FicheProduit extends EcomAction implements ServletRequestAware{
    private EcomProductType product;
    private int idProduct;
    private HttpServletRequest request;
    private List<EcomComment> commentaires;
    private List<Integer> liste;
    private int nbEtoile;
    
    public EcomProductType getProduct() {
        return product;
    }

    public List<EcomComment> getCommentaires() {
        return commentaires;
    }

    public void setIdProduct(int idProduct) {
        this.idProduct = idProduct;
    }

    public int getIdProduct() {
        return idProduct;
    }

    public void setCommentaires(List<EcomComment> commentaires) {
        this.commentaires = commentaires;
    }

    public void setProduct(EcomProductType product) {
        this.product = product;
    }

    public List<Integer> getListe() {
        return liste;
    }

    public void setListe(List<Integer> liste) {
        this.liste = liste;
    }

    public int getNbEtoile() {
        return nbEtoile;
    }

    public void setNbEtoile(int nbEtoile) {
        this.nbEtoile = nbEtoile;
    }
    
    @Override
    public void setServletRequest(HttpServletRequest request) {
	this.request=request;
    }
 
    @Override
    public String execute() {
        try {
            ServiceProductRemote serviceProductRemote = lookupServiceProductRemote();
            idProduct = (int) Double.parseDouble(request.getParameter("idProduct"));
            product = serviceProductRemote.getProductType(idProduct);

            registerRate();
            
            int note = product.getScore().getScore();
            System.out.println("feff___"+idProduct+" "+product+" "+note);
            if(note==0){
            nbEtoile = 0;
            }else if(note>0 && note <=20){
                nbEtoile = 1;
            }else if(note>20 && note <=40){
                nbEtoile = 2;
            }else if(note>40 && note <=60){
                nbEtoile = 3;
            }else if(note>60 && note <=80){
                nbEtoile = 4;
            }else if(note>80 && note <=100){
                nbEtoile = 5;
            }

            commentaires = serviceProductRemote.getComments(idProduct, 0, 10);
            if(commentaires == null){
                setMessage("Soyez le premier à écrire un avis.");
            }
            liste= new ArrayList<Integer>();
            int quantity = serviceProductRemote.getAvailableQuantity(idProduct);
            for (int i = 1; i <= quantity; i++)
            {
                liste.add(i);
                if (i == 10)
                {
                    break;
                }
            }
            return SUCCESS;
        } catch (EcomQueryException ex) {
            Logger.getLogger(FicheProduit.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EcomException ex) {
            Logger.getLogger(FicheProduit.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return ERROR;
    }
    
}
